package app.web.galgame.collects.auto;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import app.client.chs.PinYinUtils;
import app.esb.common.FileTools;
import app.esb.common.NumberTools;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.lang.Basic;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.lang.number.MathUtil;
/**
 * 明细分析
 * @author JiaJ
 *
 */
public class GalAutoCollectDetail {
	
	private String fileName;
	
	private String fileDate;
	
	private File folder;
	
	private String gameName;
	
	private String gameID;
	
	private String gameType = "ADV";
	
	private String gameLang = "02";
	
	private String gameVisual = "01";
	
	private String gameSize = "";
	
	private String gameFactory = "";
	
	private String gameMemo = "";
	
	private String gameDate = "";
	
	private String gameNamePlus = "";
	
	private String diskID = "H3";
	
	private String gameStorePath = "";
	
	private String gameTags = "";
	
	private Set<String> gameNameSet = new HashSet<String>();
	
	private Set<String> gameTagSet = new HashSet<String>();

	public void setFileName(String fileName) {
		this.fileName = this.getString(fileName);
	}

	public void setFileDate(String fileDate) {
		this.fileDate = fileDate;
	}

	public void setFolder(File folder) {
		this.folder = folder;
	}
	
	public void analyse(){
		
		try {
			
			System.out.println("【分析】"+fileName);
			
			if(this.analyseOringnal()){
				
				//HgameCN分析
				this.analyseHgameCN();
								
				//喵窝分析
				this.analyseYuiNya();
				
				//Bangumi分析
				this.analyseBungumi();
				
				//存在匹配数据
				if(this.gameNameSet.size()>0){
					
					this.analyseData();
					
					this.insertData();
					
					this.moveFile();
					
				}
				
				//未匹配成功
				else{
					
					this.removeToError();
					
				}
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private boolean analyseOringnal(){
		
		String dateF = BusinessDate.formatDate(this.fileDate);
		
		String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
								+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
								+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
								+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
								+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
								+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
						 + "from ${schema}.LEO_GALGAME_INFO_JPS "
						 + "where ( upper(GAME_NAME) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "' or upper(GAME_NAME_PLUS) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "')";
		
		DataTable orignalTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		
		while(orignalTable.next()){
			
			String activeDate = orignalTable.getString("GAME_PRO_TIME");
			
			if(activeDate.length() > 10){
				activeDate = activeDate.substring(0, 10);
			}
			
			if(dateF.equals(activeDate)){
				
				double gameSizeStore = NumberTools.toKBSize(orignalTable.getString("GAME_SIZE"));
				double gameSizeActive = NumberTools.toKBSize(FileTools.getFileSize(folder.getPath().replaceAll("\\\\", "/")));
				
				if(gameSizeActive > gameSizeStore){
					this.removeToBig();
				}else{
					this.removeToExist();
				}
					
				return false;
			}
			
		}
		
		return true;
		
	}
	
	private void analyseHgameCN(){
		String dateF = BusinessDate.formatDate(this.fileDate);
		String whereSQL = "";
		whereSQL += " UPPER(GAME_NAME) like '" + Basic.toWhereLike(this.fileName.trim().toUpperCase())+"'";
		whereSQL += " or UPPER(GAME_NAME_OTH) like '" + Basic.toWhereLike(this.fileName.trim().toUpperCase())+"'";
		String sqlString = "select GAME_NAME,GAME_NAME_OTH,GAME_FACTORY,"
		         + "to_char(GAME_DATE,'yyyy-mm-dd') GAME_DATE,"
		         + "GAME_TAG,GAME_ID,GAME_PICTOR,GAME_WRITER,GAME_MEMO,"
		         + "GAME_URL,GAME_CAST,GAME_MUSIC,GAME_SINGER "
		         + "from ${schema}.LEO_HGAMECN_INFO "
		         + "where "+whereSQL;
		
		DataTable hgameCNTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		while(hgameCNTable.next()){
			String gameDate = BusinessDate.formatDate(hgameCNTable.getString("GAME_DATE"));
			if(dateF.equals(gameDate)){
				
				System.out.println("【匹配成功】HgameCN");
				
				String gameName = this.getString(hgameCNTable.getString("GAME_NAME"));
				String gameNameOth = this.getString(hgameCNTable.getString("GAME_NAME_OTH"));
				String gameTag = this.getString(hgameCNTable.getString("GAME_TAG"));
				String gameMemo = this.getString(hgameCNTable.getString("GAME_MEMO"));
				String gameFactory = this.getString(hgameCNTable.getString("GAME_FACTORY"));
				
				this.gameNameSet.add(gameName);
				if(gameNameOth!=null && !"".equals(gameNameOth)){
					for(String name:gameName.split(",")){
						this.gameNameSet.add(name);
					}
				}
				if(gameTag!=null && !"".equals(gameTag)){
					for(String tag:gameTag.split(",")){
						this.gameTagSet.add(tag);
					}
				}
				
				if(gameMemo!=null && !"".equals(gameMemo)){
					if(this.gameMemo == null ||"".equals(this.gameMemo)){
						this.gameMemo = gameMemo;
					}
				}
				
				if(gameFactory!=null && !"".equals(gameFactory)){
					if(this.gameFactory == null ||"".equals(this.gameFactory)){
						this.gameFactory = gameFactory;
					}
				}
				
				break;
			}
			
		}
		
	}
	
	private void analyseYuiNya(){
		String dateF = BusinessDate.formatDate(this.fileDate);
		String whereSQL = "";
		whereSQL += " UPPER(GAME_NAME) like '" + Basic.toWhereLike(this.fileName.trim().toUpperCase())+"'";
		String sqlString = "select * from ${schema}.LEO_YUINYA_GAL_INFO "
		         + "where "+whereSQL;
		
		DataTable yuiNyaTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		while(yuiNyaTable.next()){
			String gameDate = yuiNyaTable.getString("GAME_DATE");
			if(dateF.equals(gameDate)){
				
				System.out.println("【匹配成功】yuiNya");
				
				String gameName =this.getString( yuiNyaTable.getString("GAME_NAME"));
				String gameTag = this.getString(yuiNyaTable.getString("GAME_TYP"));
				String gameMemo = this.getString(yuiNyaTable.getString("GAME_MEMO"));
				
				this.gameNameSet.add(gameName);

				if(gameTag!=null && !"".equals(gameTag)){
					for(String tag:gameTag.split(",")){
						this.gameTagSet.add(tag);
					}
				}
				
				if(gameMemo!=null && !"".equals(gameMemo)){
					if(this.gameMemo == null ||"".equals(this.gameMemo)){
						this.gameMemo = gameMemo;
					}
				}
				
				break;
			}
			
		}
		
	}
	
	private void analyseBungumi(){
		String dateF = BusinessDate.formatDate(this.fileDate);
		String whereSQL = "";
		whereSQL += " UPPER(GAME_NAME) like '" + Basic.toWhereLike(this.fileName.trim().toUpperCase())+"'";
		whereSQL += " or UPPER(GAME_NAME_OTH) like '" + Basic.toWhereLike(this.fileName.trim().toUpperCase())+"'";
		String sqlString = "select * from ${schema}.LEO_BANGUMI_GAME_INFO "
		         + "where "+whereSQL;
		
		DataTable bungumiTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		while(bungumiTable.next()){
			String gameDate = BusinessDate.formatDate(bungumiTable.getString("GAME_DATE"));
			if(dateF.equals(gameDate)){
				
				System.out.println("【匹配成功】Bungumi");
				
				String gameName = this.getString(bungumiTable.getString("GAME_NAME"));
				String gameNameOth = this.getString(bungumiTable.getString("GAME_NAME_OTH"));
				String gameTag = this.getString(bungumiTable.getString("GAME_TAG"));
				String gameMemo = this.getString(bungumiTable.getString("GAME_MEMO"));
				String gameFactory = bungumiTable.getString("GAME_MAKER");
				
				this.gameNameSet.add(gameName);
				if(gameNameOth!=null && !"".equals(gameNameOth)){
					for(String name:gameName.split(",")){
						this.gameNameSet.add(name);
					}
				}
				if(gameTag!=null && !"".equals(gameTag)){
					for(String tag:gameTag.split(",")){
						this.gameTagSet.add(tag);
					}
				}
				
				if(gameMemo!=null && !"".equals(gameMemo)){
					if(this.gameMemo == null ||"".equals(this.gameMemo)){
						this.gameMemo = gameMemo;
					}
				}
				
				if(gameFactory!=null && !"".equals(gameFactory)){
					if(this.gameFactory == null ||"".equals(this.gameFactory)){
						this.gameFactory = gameFactory;
					}
				}
				
				break;
			}
			
		}
		
	}
	
	private void analyseData(){
		
		System.out.println("【数据分析】"+fileName);
		
		this.gameID = System.currentTimeMillis() + MathUtil.random(3);
		this.gameDate = BusinessDate.formatDate(fileDate);
		this.gameName = this.fileName;
		this.gameSize = FileTools.getFileSize(folder.getPath().replaceAll("\\\\", "/"));
		if(this.gameMemo == null){
			this.gameMemo = "";
		}
		if(this.gameFactory == null){
			this.gameFactory = "";
		}
		
		for(String act:this.gameNameSet){
			this.gameNamePlus += "," + act; 
		}
		if(this.gameNamePlus != null && !"".equals(this.gameNamePlus)){
			this.gameNamePlus = this.gameNamePlus.substring(1);
		}
		
		for(String act:this.gameTagSet){
			this.gameTags += "," + act; 
		}
		if(this.gameTags != null && !"".equals(this.gameTags)){
			this.gameTags = this.gameTags.substring(1);
		}
		
		if(this.gameTags != null){
			if(this.gameTags.toUpperCase().indexOf("RPG")>=0){
				this.gameType += ",RPG";
			}
			if(this.gameTags.toUpperCase().indexOf("SLG")>=0){
				this.gameType += ",SLG";
			}
			if(this.gameTags.toUpperCase().indexOf("ACT")>=0){
				this.gameType += ",ACT";
			}
			if(this.gameTags.toUpperCase().indexOf("TAL")>=0){
				this.gameType += ",TCG";
			}
		}
		
		this.gameStorePath = "/"+this.fileDate.substring(0,4) + "年/" + this.fileName;
		
	}
	
	private void insertData(){
		
		System.out.println("【数据存储】"+fileName);
		
		String firstWd = PinYinUtils.getFirstWord(this.fileName);
		
		String insertSQL = "insert into ${schema}.LEO_GALGAME_INFO_JPS "
				+ "(GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,GAME_VISUAL,"
				+ "GAME_SIZE,GAME_FACTORY,GAME_INTRU,"
				+ "GAME_PRO_TIME,GAME_CRE_TIME,"
				+ "GAME_NAME_PLUS,DISK_ID,GAME_STORE_PATH,GAME_TAGS,GAME_FIRST) "
				+ "values "
				+ "('"+gameID+"','"+this.gameName+"','"+this.gameType+"','"+this.gameLang+"','"+this.gameVisual+"',"
				 + "'"+this.gameSize+"','"+this.gameFactory+"','"+this.gameMemo+"',"
				 + "to_date('"+this.gameDate+"','yyyy-mm-dd'),to_date('"+BusinessDate.getTodaytime()+"','yyyy-mm-dd hh24:mi:ss'),"
				 + "'"+this.gameNamePlus+"','"+this.diskID+"','"+this.gameStorePath+"','"+this.gameTags+"','"+firstWd+"') ";
		
		DBFactory.getThreadDB().executeUpdate(insertSQL);
		
	}
	
	private void moveFile(){
		File file = new File("G:/"+this.fileDate.substring(0,4) + "年");
		if(!file.exists()){
			file.mkdirs();
		}
		
		this.folder.renameTo(new File("G:/"+this.fileDate.substring(0,4) + "年/"+this.folder.getName()));
	}
	
	protected String getString(String src){
		
		if(src == null){
			return "";
		}
		
		return src.replaceAll("'", "’")
				  .replaceAll("\"", "”")
				  .replaceAll("&nbsp;", "")
				  .replaceAll("<br>", "")
				  .replaceAll("<br/>", "")
				  .replaceAll("<p>", "")
				  .replaceAll("</p>", "");
	}
	
	public void removeToExist(){
		this.folder.renameTo(new File("G:/待处理/ANLYSE/Exist/"+this.folder.getName()));
	}
	
	public void removeToBig(){
		this.folder.renameTo(new File("G:/待处理/ANLYSE/Big/"+this.folder.getName()));
	}
	
	public void removeToError(){
		this.folder.renameTo(new File("G:/待处理/ANLYSE/Error/"+this.folder.getName()));
	}
	
}
